﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace CrosswordConstructor
{
    class CrosswordCreator
    {
        Random random = new Random();

        private List<Word> crossword = new List<Word>();
        private List<string> wordsList = new List<string>();
        private List<string> alreadyUsedWords = new List<string>();

        private bool addingOfFirstWords = true;

        private int crosswordHeight;
        private int crosswordWidth;

        public CrosswordCreator(int crosswordHeight, int crosswordWidth, string fileName) //TODO: File name goes here
        {
            this.crosswordHeight = crosswordHeight;
            this.crosswordWidth = crosswordWidth;
      //      List<Word> words = AddWordsToWordsList(fileName); 

     //       foreach (Word word in words.OrderBy(x => Guid.NewGuid()))
            {
     //           if(CalculatePosition(word))
                {
    //                TryToAdd(word);
     //               crossword.Add(word);
                }
            }
            for (int i = 0; i < 1000; i++) //TODO: foreach the list
            {
       //         AddWordToCrossword(); //TODO: A
            }
        }

        private bool CalculatePosition(Word word)
        {
            if (addingOfFirstWords)
            {
                word.isHorizontal = true;
                word.i = crosswordHeight/2;
                word.j = crosswordWidth/2 - word.word.Length/2;
                addingOfFirstWords = false;
                return true;
            }

            int wordCount = 0;

            foreach (char c in word.word)
            {
                foreach (Word word1 in crossword.OrderBy(x => Guid.NewGuid()))
                {
                    int word1Count = 0;
                    foreach (char c1 in word1.word)
                    {
                        if (c1 == c)
                        {
                            if (word1.isHorizontal == null || word1.i == null || word1.j == null)
                                return false;
                            word.isHorizontal = !word1.isHorizontal;
                            if ((bool)word.isHorizontal)
                            {
                                if (!((word.i = word1.i - wordCount) > 0 && word.i < crosswordHeight - 1 &&
                                    ((word.j = word1.j + word1Count) > 0 && word.j < crosswordWidth - 1)))
                                    return false;
                            }
                            else
                            {
                                if (!((word.i = word1.i + word1Count) > 0 && word.i < crosswordHeight - 1 &&
                                    (word.j = word1.j - wordCount) > 0 && word.j < crosswordWidth - 1))
                                    return false;
                            }
                            return true;
                        }
                        word1Count++;
                    }
                }
               wordCount++;
            }
            return true;
        }

        private char[,] WordListToCharArray(List<Word> words)                    //Convert Word list to char array same size that crossword have
        {
            char[,] charArray = new char[crosswordHeight,crosswordWidth];

            foreach (Word word in words)
            {
                if (word.word != null && word.i != null && word.j != null && word.isHorizontal !=null)
                {
                    int? i = word.i;
                    int? j = word.j;
                    foreach (char c in word.word)
                    {
                        if ((bool)word.isHorizontal)
                            j++;
                        else
                            i++;
                        charArray[(int)i, (int)j] = c;
                    }
                }
            }
            return charArray;
        }

        //private Word BuildWord(string word, int i,int j, bool makeWordHorizontal)   // Make new word with 
        //{                                                                           //specified begin coordinates 
        //    Word tempWord = new Word();                                             //
        //    tempWord.symbols= new List<Symbol>();
        //    Symbol tempSymbol=new Symbol();

        //    tempWord.isHorizontal = makeWordHorizontal;

        //    if (makeWordHorizontal)
        //    {   
        //       for (int k = j; k < word.Length+j; k++)
        //        {
        //            tempSymbol.j = k;
        //            tempSymbol.i = i;
        //            tempSymbol.symbol = word[k - j];
        //            tempWord.symbols.Add(tempSymbol);
        //        }
                
        //    }
        //    else
        //    {
        //        for (int k = i; k < word.Length + i; k++)
        //        {
        //            tempSymbol.j = j;
        //            tempSymbol.i = k;
        //            tempSymbol.symbol = word[k - i];
        //            tempWord.symbols.Add(tempSymbol);
        //        }
        //    }

        //    return tempWord;
        //}

        //private void AddWordToCrossword()            //find word with same char 
        //{
        //    string nextWordTryToBeAdded = NextWordFromWordsList();
        //    int count=0;
        //    Word tempWord=new Word();
        //    bool add = false;
        //    if (addingOfFirstWords)
        //    {
        //        AddFirstWords();
        //       // add = true;
        //        addingOfFirstWords = false;
        //    }
        //    else
        //    {
        //        foreach (char c in nextWordTryToBeAdded)
        //        {
        //            for (int i = 0; i < crossword.Count; i++)
        //            {
        //                Word word = crossword[random.Next(crossword.Count)];
        //                for (int j = 0; j < word.symbols.Count; j++)
        //                {
        //                    Symbol symbol = word.symbols[random.Next(word.symbols.Count)];


        //                    if (symbol.symbol != c) continue;
        //                    int addedWordI;
        //                    int addedWordJ;
        //                    add = true;
        //                    if (word.isHorizontal)
        //                    {

        //                        addedWordJ = symbol.j;
        //                        addedWordI = symbol.i - count;
        //                        tempWord = BuildWord(nextWordTryToBeAdded, addedWordI, addedWordJ, false);
        //                    }
        //                    else
        //                    {
        //                        addedWordI = symbol.i;
        //                        addedWordJ = symbol.j - count;
        //                        tempWord = BuildWord(nextWordTryToBeAdded, addedWordI, addedWordJ, true);
        //                    }

        //                }

        //            }

        //            count++;
        //        }
        //    }
        //    if (add)
        //    TryToAdd(tempWord);
        //}

        //private void TryToAdd(Word word)                            //calculate possibility of adding Word to crossword
        //{                                                           //and if possible - add Word to crossword
        //    foreach (Symbol symbol in word.symbols)                 //if added - add to AlreadyUsedWordsList
        //    {
        //        if (symbol.i < 0 || symbol.j < 0 || symbol.i > crosswordHeight - 1 || symbol.j > crosswordWidth - 1)
        //            return;
        //    }
        //    List<Word> addedWordList=new List<Word>();
        //    addedWordList.Add(word);                                                              
        //    char[,] crosswordArray = WordListToCharArray(crossword);                                 
        //    char[,] wordArray = WordListToCharArray(addedWordList);   
            
                                                                   
        //    for (int i = 0; i < crosswordHeight; i++)
        //    {
        //        for (int j = 0; j < crosswordWidth; j++)
        //        {
        //            if (wordArray[i,j]!='\0')
        //            if (!(crosswordArray[i,j]==wordArray[i,j] || crosswordArray[i,j]=='\0'))
        //           return;
        //        }
        //    }
            
        //    alreadyUsedWords.Add(word.ToString());
        //    crossword.Add(word);
        //}
        



        public void ViewCrossword()
        {

            char[,] charArray = WordListToCharArray(crossword);
            for (int i = 0; i < crosswordHeight; i++)
            {
                for (int j = 0; j < crosswordWidth; j++)
                {
                    Console.Write(charArray[i,j]);
                }
                Console.WriteLine();
            }
            Console.ReadKey();
        }

        public List<Word> ReturnCrossword()
        {
           return crossword;
        }

    }
}
